<?php

class sxTransactionRequest extends baseTransactionRequestObject {

    private $usepay;
    private $token;

    public function __construct($wsdl, $sourceKey, $pin) {
  
        //set the client wsdl
        $this->merchantObj = new sxMerchant($wsdl, $sourceKey, $pin);
        //create instance of the token and from the webservice
        $this->usepay = $this->merchantObj->getClient();
        $this->token = $this->merchantObj->getToken();
    }

    /*
     * This method duplicates the functionality of the gateway API.
     *  It can be used to run a wide variety of transaction types 
     * including sales, credits, authonly, void, and checks. 
     */

    public function runTransaction() {
        try {
            $results = $this->usepay->runTransaction($this->token, $this);
        } catch (Exception $exc) {
            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * This method is equivalent to running the runTransaction method with the Command set to “Sale.” 
     */

    public function runSale() {
        try {
            $results = $this->usepay->runSale($this->token, $this);
        } catch (Exception $exc) {
            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * This method is equivalent to running the runTransaction method with the Command set to “AuthOnly.” 
     */

    public function runAuthOnly() {
        try {
            $results = $this->usepay->runAuthOnly($this->token, $this);
        } catch (Exception $exc) {
            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * This method refunds a sale. 
     * Both credit card and check sale transactions may be refunded.
     *  You may choose to refund the entire amount or just a portion of the original sale. 
     */

    public function refundTransaction($refNum, $amount) {
        try {
            $results = $this->usepay->refundTransaction($this->token, $refNum, $amount);
        } catch (Exception $exc) {

            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * Move an authorized transaction into the current batch for settlement.
     *  It is possible to capture an amount other than the one originally authorized,
     *  however, you must follow the guidelines established by the merchant service bank. 
     * Capturing a higher or lower dollar amount could result in additional penalties and fees. 
     */

    public function captureTransaction($refNum, $amount) {
        try {
            $results = $this->usepay->captureTransaction($this->token, $refNum, $amount);
        } catch (Exception $exc) {

            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    /*
     * This method runs a Credit transaction in order to refund all
     *  or part of a previous Sale, or issue an open credit to a customer's credit card. 
     */

    public function runCredit() {
        try {
            $results = $this->usepay->runCredit($this->token, $this);
        } catch (Exception $exc) {

            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

    public function getTransactionStatus($refNum) {
        try {
            $results = $this->usepay->getTransactionStatus($this->token, $refNum);
        } catch (Exception $exc) {

            throw new Exception(usaepayErrorHandlee::getError($exc->getMessage()));
        }
        return $results;
    }

}